<?php
namespace Ucmaim\Controller\Home;

class SalenewController extends CommonController {

    public function _initialize(){
        parent::_initialize();
        $this->requireLogin();
    }
    //发布商品
    public function salenew(){
        if( IS_GET ){
            $uid=session("member_info");
            $data['member_id']=$uid['uid'];
            $this->member=M('member')->where($data)->find();
            $category=M('goods_category');
            $this->mark=$category->where("gc_pid=0")->select();
            $this->display('Home/Salenew');
        }else{
                if($_FILES["cover"]["name"][0]){
                    $config = array(
                        'maxSize'    =>    3145728,
                        'rootPath'   =>    './Application/Ucmaim/Uploads/',
                        'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                        'subName'    =>    array('date','Ymd'),
                    );
                    $upload = new \Think\Upload($config);
                    $info   =   $upload->upload();
                    if(!$info){
                        $this->error($upload->getError());
                    }
                    $pitcure=array();
                    foreach($info as $v){
                        $pitcure[] = $v['savepath'].$v['savename'];
                    }
                    $data['goods_pic']=implode(',',$pitcure);
                }else{
                    //$data['goods_pic']='coin.png';
                }
            $rules = array(
                array('gc_id','require','必须选择游戏服务器！'),
                array('goods_typeid','require','必须选择物品类型！'),
                array('goods_title','1,40','请输入1-40字商品标题！',3,'length'),
                array('goods_price','require','必须填写商品单价！'),
                array('goods_price', '/^[0-9.]+$/' , '商品单价必须是数字' ,1, 'regex' ,1),
                array('goods_amount','require','必须填写商品发布件数！'),
                array('goods_amount', '/^[0-9]+$/' , '商品发布件数必须是数字' ,1, 'regex' ,1),
                array('goods_content','1,500','请输入1-500字商品描述！',3,'length'),
                array('goods_mobile','11','请输入正确的手机号码！',3,'length'),
                array('goods_qq','5,11','请输入正确的QQ号码！',3,'length'),
                array('goods_tao','require','必须填写游乐淘帐号！'),
                array('goods_alipay','require','必须填写支付宝帐号！'),
            );
            $goods_price=I('post.goods_price');
            if($goods_price<0.01){
                $this->error('必须填写大于0.01商品单价！');
            }
            $shop=M('goods');
            if (!$shop->validate($rules)->create()){
                   exit($shop->getError());
            }else{
                $gcid=I('post.gc_id');
                if($gcid==0){
                    $this->error('必须选择游戏服务器');
                }
                $data['gc_id']=$gcid;
                $data['goods_typeid']=I('post.goods_typeid');
                $data['goods_title']=I('post.goods_title');
                $data['goods_price']=$goods_price;
                $data['goods_amount']=I('post.goods_amount');
                $data['goods_storage']=I('post.goods_amount');
                $data['goods_content']=I('post.goods_content');
                $data['goods_mobile']=I('post.goods_mobile');
                $data['goods_qq']=I('post.goods_qq');
                $data['goods_alipay']=I('post.goods_alipay');
                $data['goods_tao']=I('post.goods_tao');
                $data['goods_time']=time();
                $uid=session("member_info");
                $data['member_id']=$uid['uid'];//发布者ID
                $data['goods_status']=0;
                $mark=$shop->data($data)->add();
                if($mark){
                    $this->success('发布成功');
                }else{
                    $this->error('发布失败');
                }
            }
        }

    }
    public function salenewajax(){
        $category=M('goods_category');
        if( IS_AJAX ){
            $id=I('post.lx');
            if($id!=0){
                $mark=$category->where("gc_pid=$id")->select();
                echo json_encode($mark);
            }
        }else{
            $mark=$category->where("gc_pid=0")->select();
            $this->assign('mark',$mark);
            $this->display();
        }

    }
    public function wplxajax(){
        if( IS_AJAX ){
            $id=I('post.wplx');
            $type=M('goods_type');
            $mark=$type->where("gc_id=$id")->select();
            echo json_encode($mark);
        }
    }
}
